package com.iss.examsys.view.util;

import com.iss.examsys.common.EmsConstants;
import com.iss.examsys.entity.SectionTag;
import com.iss.examsys.ejb.SubjectTagBean;
import com.iss.examsys.entity.Module;
import java.util.Collection;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;

public class JsfUtil {
      
    public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {
        int size = selectOne ? entities.size() + 1 : entities.size();
        SelectItem[] items = new SelectItem[size];
        int i = 0;
        if (selectOne) {
            items[0] = new SelectItem("", "---");
            i++;
        }
        for (Object x : entities) {
            items[i++] = new SelectItem(x, x.toString());
        }
        return items;
    }

    public static void addErrorMessage(Exception ex, String defaultMsg) {
        String msg = ex.getLocalizedMessage();
        if (msg != null && msg.length() > 0) {
            addErrorMessage(msg);
        } else {
            addErrorMessage(defaultMsg);
        }
    }

    public static void addErrorMessages(List<String> messages) {
        for (String message : messages) {
            addErrorMessage(message);
        }
    }

    public static void addErrorMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
        FacesContext.getCurrentInstance().addMessage(null, facesMsg);
    }

    public static void addSuccessMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
        FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg);
    }

    public static String getRequestParameter(String key) {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);
    }

    public static Object getObjectFromRequestParameter(String requestParameterName, Converter converter, UIComponent component) {
        String theId = JsfUtil.getRequestParameter(requestParameterName);
        return converter.getAsObject(FacesContext.getCurrentInstance(), component, theId);
    }
    
    public static SelectItem[] getSectionTypeItems() {
        SelectItem[] items = new SelectItem[3];
        items[0] = new SelectItem("", "---");
        items[1] = new SelectItem("Auto", "Auto");
        items[2] = new SelectItem("Manual", "Manual");        
        return items;
    }
    
    public static String getCommaSeparatedTags(Collection<SectionTag> sectionTags) {
        StringBuilder sb = new StringBuilder();
        for(SectionTag sectionTag : sectionTags) {
            sb.append(sectionTag.getTagId().getName());      
            sb.append(", ");
        }
        return sb.toString().substring(0, sb.length() -2);
    }
    
    public static SelectItem[] getSelectQuestionTypeItems() {
        SelectItem[] items = new SelectItem[3];
        items[0] = new SelectItem(EmsConstants.QUESTION_TYPE_ESSAY, "Essay");
        items[1] = new SelectItem(EmsConstants.QUESTION_TYPE_MULTI_CHOICE_MULTI_ANS, "Multiple choices - multiple select");
        items[2] = new SelectItem(EmsConstants.QUESTION_TYPE_MULTI_CHOICE_SINGLE_ANS, "Multiple choices - single select");
      
        return items;
    }
}
